package com.jdy.haoduoai.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * 柱状图
 * 
 * @author
 * 
 */
public class HistogramView2 extends View {

  private Paint xLinePaint;// 坐标轴 轴线 画笔：
  private Paint hLinePaint;// 坐标轴水平内部 虚线画笔
  private Paint titlePaint;// 绘制文本的画笔

  private int[] progress = { 10000, 2500, 5000, 3000 };
  private int[] aniProgress;// 实现动画的值
  private int[] text;// 设置点击事件，显示哪一条柱状的信息
  private String[] ySteps;
  private int flag;// 是否使用动画

  private HistogramAnimation ani;

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    setMeasuredDimension(dp2px(25), dp2px(160));
  }

  public HistogramView2(Context context) {
    super(context);
    init();
  }

  public HistogramView2(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  private boolean showCoordinate = true;

  public boolean isShowCoordinate() {
    return showCoordinate;
  }

  public void setShowCoordinate(boolean showCoordinate) {
    this.showCoordinate = showCoordinate;
  }

  private void init() {

    ySteps = new String[] { "360", "", "180", "", "Oh" };
    text = new int[] { 0, 0, 0, 0, 0, 0, 0 };
    aniProgress = new int[] { 100, 20, 200, 150 /* , 0, 0, 0 */};

    ani = new HistogramAnimation();
    ani.setDuration(2000);

    xLinePaint = new Paint();
    hLinePaint = new Paint();
    titlePaint = new Paint();

    // 给画笔设置颜色
    xLinePaint.setColor(Color.DKGRAY);
    hLinePaint.setColor(Color.LTGRAY);
    titlePaint.setColor(Color.BLACK);
  }

  public void start(int flag) {
    this.flag = flag;
    this.startAnimation(ani);
  }

  @SuppressLint("DrawAllocation")
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int height = getHeight() - dp2px(30);
    int leftHeight = height - dp2px(5);// 左侧外周的 需要划分的高度：
    int hPerHeight = leftHeight / 4;// 分成四部分

    hLinePaint.setTextAlign(Align.CENTER);

    titlePaint.setTextAlign(Align.RIGHT);
    titlePaint.setTextSize(sp2px(12));
    titlePaint.setAntiAlias(true);
    titlePaint.setStyle(Paint.Style.FILL);
    // 设置左部的数字
    if (isShowCoordinate()) {
      for (int i = 0; i < ySteps.length; i++) {
        canvas.drawText(ySteps[i], dp2px(25), dp2px(13) + i * hPerHeight,
            titlePaint);
      }
    }
  }

  private int dp2px(int value) {
    float v = getContext().getResources().getDisplayMetrics().density;
    return (int) (v * value + 0.5f);
  }

  private int sp2px(int value) {
    float v = getContext().getResources().getDisplayMetrics().scaledDensity;
    return (int) (v * value + 0.5f);
  }

  /**
   * 设置点击事件，是否显示数字
   */
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    int step = (getWidth() - dp2px(30)) / 8;
    int x = (int) event.getX();
    for (int i = 0; i < 7; i++) {
      if (x > (dp2px(15) + step * (i + 1) - dp2px(15))
          && x < (dp2px(15) + step * (i + 1) + dp2px(15))) {
        text[i] = 1;
        for (int j = 0; j < 7; j++) {
          if (i != j) {
            text[j] = 0;
          }
        }
        if (Looper.getMainLooper() == Looper.myLooper()) {
          invalidate();
        } else {
          postInvalidate();
        }
      }
    }
    return super.onTouchEvent(event);
  }

  private class HistogramAnimation extends Animation {
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
      super.applyTransformation(interpolatedTime, t);
      if (interpolatedTime < 1.0f && flag == 2) {
        for (int i = 0; i < aniProgress.length; i++) {
          aniProgress[i] = (int) (progress[i] * interpolatedTime);
        }
      } else {
        for (int i = 0; i < aniProgress.length; i++) {
          aniProgress[i] = progress[i];
        }
      }
      invalidate();
    }
  }

}